import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {cacheTools} from 'nc-lightapp-front';

export default function getJobHistoryData(pk_psnjob) {
    const {orgVal} = this.props;

    const {modal: {show}, table: {setAllTableData}, updateState} = this.props;

    let postData = {
        pk_org: orgVal.refpk,
        func_type: 3,
        pk_psnjob
    };

    const allpks = cacheTools.get('allpks') || [];

    return proFetch({
        url: '/nccloud/hrhi/psndoc/PsndocQueryJobHistroyAction.do',
        body: postData
    }).then((res) => {
        if (res.success) {
            show('jobHistory');
            setAllTableData('hi_psnjob_history', {rows: res.data.currentJobs.hi_psnjob.rows, allpks});
            if (res.data.histroyJobs) {
                updateState(
                    {
                        showHistoryTable: true
                    },
                    () => {
                        setAllTableData('hi_psnjob_history_down', {
                            rows: res.data.currentJobs.hi_psnjob.rows
                        });
                        cacheTools.set('allpks', allpks);
                    }
                );
            } else {
                updateState(
                    {
                        showHistoryTable: false
                    },
                    () => {
                        setAllTableData('hi_psnjob_history_down', {rows: []});
                        cacheTools.set('allpks', allpks);
                    }
                );
            }
        }
    });
}
